// pages/mine/fish/putfish.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageList: [],
    attach: [],
    fishInfo: {
      fields: {},
      albums: []
    },
    src: '',
    showVideoModal:false,
    video_src:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    var _fishInfo = that.data.fishInfo;
    if (options.id) {
      _fishInfo.id = options.id
      that.getFishDetail(_fishInfo.id);
    }

  },
  getFishDetail: function(id) {
    wx.showLoading({
      title: '正在加载信息',
    })
    var that = this;
    wx.request({
      url: app.globalData.Domain + '/fish/detail',
      data: {
        id: id
      },
      success: function(res) {
        if (res.data.code == 0) {
          var _detail = res.data.data;
          var albums = _detail.albums;
          var imgs = [];
          for (var i = 0; i < albums.length; i++) {
            imgs.push(app.globalData.Domain + albums[i].original_path);
          }
          console.log(_detail)
          that.setData({
            fishInfo: _detail,
            imageList: imgs,
            video_src: _detail.fields.video_src
          });
        } else {
          wx.showModal({
            title: '哪有鱼提示',
            content: res.data.msg,
            showCancel: false
          })
        }
      },
      complete: function() {
        wx.hideLoading()
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  deleteImg: function(e) {
    var that = this
    var index = e.target.dataset.index
    var tempList = that.data.imageList;
    tempList.splice(index, 1);
    that.setData({
      imageList: tempList
    })
  },
  chooseImage: function() {
    var that = this
    wx.chooseImage({
      count: 1,
      success: function(res) {
        console.log(res)
        var tempList = that.data.imageList;
        tempList.push(res.tempFilePaths[0]);
        that.setData({
          imageList: tempList
        })
       // console.log(tempList);
      }
    })
  },
  previewImage: function(e) {
    var current = e.target.dataset.src

    wx.previewImage({
      current: current,
      urls: this.data.imageList
    })
  },
  chooseVideo: function() {
    var that = this
    wx.chooseVideo({
      sourceType: ['camera', 'album'],
      camera: ['front', 'back'],
      maxDuration: 60,
      success: function(res) {
        that.setData({
          src: res.tempFilePath
        })
      }
    })
  },
  chooseQQVideo: function() {
    this.setData({
      showVideoModal: true
    });
  },
  puttap: function() {
    var that = this;
    var imgs = that.data.imageList;
    wx.showLoading({
      title: '正在保存图片...',
    })
    var _token = app.globalData.token;
    var _fishinfo = that.data.fishInfo;
    var _imgpaths = _fishinfo.albums;
    var upImgs = [];
    for (var i = 0; i < imgs.length; i++) {
      if (imgs[i].indexOf("http://tmp") >= 0) {
        upImgs.push(imgs[i]);
      }
    }
    if (upImgs.length > 0) {
      var _succ = 0;
      for (var i = 0; i < upImgs.length; i++) {
        wx.uploadFile({
          url: app.globalData.Domain + '/fish/upload', //仅为示例，非真实的接口地址
          filePath: upImgs[i],
          name: 'file',
          formData: {
            token: _token
          },
          header: {
            'content-type': 'application/json'
          },
          success: function(res) {
            var data = JSON.parse(res.data);
            console.log(res);
            _succ++;
            _imgpaths.push(data.data);
            if (_succ == upImgs.length) {
              //开始保存鱼讯
              _fishinfo.albums = _imgpaths;
              that.doput(_fishinfo);
            }
          }
        })
      }
    } else {
      that.doput(_fishinfo);
    }
  },
  doput: function(obj) {
    var that = this;
    var _token = app.globalData.token;
    wx.request({
      method: 'POST',
      url: app.globalData.Domain + '/fish/putfish', //接口地址
      data: {
        token: _token,
        model: obj
      },
      header: {
        'content-type': 'application/json'
      },
      success: function(res) {
        if (res.data.code == 0) {
          wx.showToast({
            title: '信息修改成功',
            duration: 2000
          })
          setTimeout(function() {
            wx.navigateBack();
          }, 2000)
        } else {
          wx.showModal({
            title: '哪有鱼提示',
            content: res.data.msg
          })
        }
      },
      fail: function(res) {
        console.log(res);
        wx.showModal({
          title: '哪有鱼提示',
          content: res.errMsg
        })
      },
      complete: (res) => {
        wx.hideLoading();
        that.setData({
          btn_submit: false
        });
      }
    })
  },
  hideModal:function(){
    this.setData({
      showVideoModal: false
    });
  },
  videoInput:function(e){
    var _fishinfo = this.data.fishInfo;
    _fishinfo.fields.video_src = e.detail.value;
    this.setData({
      fishInfo: _fishinfo
    });
  }
})